package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.constants.message.HotArticleConstants;
import com.heima.model.mess.app.NewBehaviorDTO.BehaviorType;

import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //收藏行为记录
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        //判断操作类型
        if (dto.getOperation() == (short) 0) {
            //收藏,查询收藏是否存在
            Query query = Query.query(Criteria.where("entryId").is(dto.getEquipmentId().toString()).and("articleId").is(dto.getArticleId()));
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            //非空判断
            if (apCollection != null) {
                //已经收藏
                CustException.cust(AppHttpCodeEnum.DATA_EXIST, "您已经收藏该文章了哦!");
            } else {
                //收藏不存在
                apCollection = new ApCollection();
                apCollection.setCollectionTime(new Date());
                apCollection.setArticleId(dto.getArticleId());
                apCollection.setEntryId(dto.getEquipmentId().toString());
                apCollection.setType(dto.getType());
                //插入数据库
                mongoTemplate.save(apCollection);
            }
        } else {
            //取消收藏,查询收藏是否存在
            Query query = Query.query(Criteria.where("entryId").is(dto.getEquipmentId().toString()).and("articleId").is(dto.getArticleId()));
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            //非空判断
            if (apCollection == null) {
                //不存在收藏
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "您还未收藏该文章,无法取消!");
            } else {
                //有收藏,删除收藏
                mongoTemplate.remove(apCollection);
            }
        }
        //创建NewBehaviorDTO,发送MQ消息
        NewBehaviorDTO newBehaviorDTO = new NewBehaviorDTO();
        newBehaviorDTO.setType(BehaviorType.COLLECTION);
        newBehaviorDTO.setArticleId(dto.getArticleId());
        newBehaviorDTO.setAdd(dto.getOperation() == 0 ? 1 : -1);
        //发送MQ消息
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(newBehaviorDTO));
        //返回调用者
        return ResponseResult.okResult();
    }
}
